/**
 * @project_name staff_vehicle
 * @package_name com.ssh.dao
 * @desc TODO
 * @param
 * @author jack
 * @date 2013-4-25
 * @time 上午10:39:02
 */
package com.ssh.dao;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.SessionFactory;

import com.ssh.entity.FcarInout;

/**
 * @author jack
 *
 */
public class FcarInoutDAOImpl implements FcarInoutDAOI {
	private SessionFactory sessionFactory;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarInoutDAOI#addFcarInout(com.ssh.entity.FcarInout)
	 */
	@Override
	public FcarInout addFcarInout(FcarInout fcarInout) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().save(fcarInout);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return fcarInout;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarInoutDAOI#addFcarInoutList(java.util.List)
	 */
	@Override
	public int addFcarInoutList(List<FcarInout> fcarInoutList) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarInoutDAOI#countById(java.lang.String)
	 */
	@Override
	public int countById(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarInoutDAOI#deleteByFCardIdId(java.lang.String)
	 */
	@Override
	public int deleteByFCardId(String fCardId) {
		// TODO Auto-generated method stub
		int num;
		try {
			String hql = "delete FcarInout as f where f.fcarCardId=?";
			num = sessionFactory.getCurrentSession().createQuery(hql)
					.setString(0, hql).executeUpdate();
		} catch (Exception e) {
			return 0;
		}
		return num;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ssh.dao.FcarInoutDAOI#deleteByFcarInout(com.ssh.entity.FcarInout)
	 */
	@Override
	public int deleteByFcarInout(FcarInout fcarInout) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarInoutDAOI#deleteById(java.lang.String)
	 */
	@Override
	public int deleteById(String id) {
		// TODO Auto-generated method stub
		int num;
		try {
			String hql = "delete FcarInout as f where f.id=?";
			num = sessionFactory.getCurrentSession().createQuery(hql)
					.setString(0, hql).executeUpdate();
		} catch (Exception e) {
			return 0;
		}
		return num;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ssh.dao.FcarInoutDAOI#deleteFcarInoutList(com.ssh.entity.FcarInout)
	 */
	@Override
	public int deleteFcarInoutList(FcarInout fcarInout) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarInoutDAOI#queryById(java.lang.String)
	 */
	@Override
	public FcarInout queryById(String id) {
		// TODO Auto-generated method stub
		try {
			String hql = "from FcarInout as f where f.fcarcardId=?";
			return (FcarInout) sessionFactory.getCurrentSession()
					.createQuery(hql).setString(0, id).uniqueResult();
		} catch (Exception e) {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarInoutDAOI#queryByInTime(java.sql.Timestamp)
	 */
	@Override
	public FcarInout queryByInTime(Timestamp timestamp) {
		// TODO Auto-generated method stub
		try {
			String hql = "from FcarInout as f where f.getIn=?";
			return (FcarInout) sessionFactory.getCurrentSession()
					.createQuery(hql).setTimestamp(0, timestamp).uniqueResult();
		} catch (Exception e) {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarInoutDAOI#queryByDateTime(java.sql.Timestamp)
	 */
	@Override
	public FcarInout queryByOutTime(Timestamp timestamp) {
		// TODO Auto-generated method stub
		try {
			String hql = "from FcarInout as f where f.getIn=?";
			return (FcarInout) sessionFactory.getCurrentSession()
					.createQuery(hql).setTimestamp(0, timestamp).uniqueResult();
		} catch (Exception e) {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarInoutDAOI#queryByParam(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<FcarInout> queryByParam(String fcarCardId) {
		// TODO Auto-generated method stub
		try {
			String hql = "from FcarInout as f where f.fcarCardId=?";
			return sessionFactory.getCurrentSession().createQuery(hql)
					.setString(0, fcarCardId).list();
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * @param sessionFactory
	 *            the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ssh.dao.FcarInoutDAOI#updateByFcarInout(com.ssh.entity.FcarInout)
	 */
	@Override
	public FcarInout updateByFcarInout(FcarInout fcarInout) {
		// TODO Auto-generated method stub
		try{
			sessionFactory.getCurrentSession().update(fcarInout);
		}
		catch(Exception e){
			return null ;
		}
		return fcarInout;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.FcarInoutDAOI#updateByFcarInoutList(java.util.List)
	 */
	@Override
	public int updateByFcarInoutList(List<FcarInout> fcarInout) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.FcarInoutDAOI#queryByInOutTime(java.sql.Timestamp, java.sql.Timestamp)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<FcarInout> queryByInTime(Timestamp miniInTime,Timestamp maxInTime) {
		// TODO Auto-generated method stub

		try{
			String hql = "from FcarInout as f where f.getIn between ? and ?";
			 return sessionFactory.getCurrentSession().createQuery(hql).
			               setParameter(0, miniInTime).setParameter(1, maxInTime).list();
		}
		catch(Exception e){
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.FcarInoutDAOI#queryByInTime(java.lang.String, java.sql.Timestamp, java.sql.Timestamp)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<FcarInout> queryByInTime(String fcardId, Timestamp miniInTime,
			Timestamp maxInTime) {
		// TODO Auto-generated method stub
		try{
			String hql = "from FcarInout as f where (f.getIn between ? and ?) and f.fcarCardId = ?";
			 return sessionFactory.getCurrentSession().createQuery(hql).
			               setParameter(0, miniInTime).setParameter(1, maxInTime).
			               setParameter(0, fcardId).list();
		}
		catch(Exception e){
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.FcarInoutDAOI#queryByOutTime(java.sql.Timestamp, java.sql.Timestamp)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<FcarInout> queryByOutTime(Timestamp miniInTime,
			Timestamp maxInTime) {
		// TODO Auto-generated method stub
		String hql = "from FcarInout as f where f.getOut between ? and ?";
		 return sessionFactory.getCurrentSession().createQuery(hql).
		               setParameter(0, miniInTime).list();
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.FcarInoutDAOI#queryByOutTime(java.lang.String, java.sql.Timestamp, java.sql.Timestamp)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<FcarInout> queryByOutTime(String fcardId, Timestamp miniInTime,
			Timestamp maxInTime) {
		// TODO Auto-generated method stub
		try{
			String hql = "from FcarInout as f where (f.getOut between ? and ?) and f.fcarCardId = ?";
			 return sessionFactory.getCurrentSession().createQuery(hql).
			               setParameter(0, miniInTime).setParameter(1, maxInTime).
			               setParameter(2, fcardId).list();
		}
		catch(Exception e){
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.FcarInoutDAOI#queryByInOutTime(java.lang.String, java.sql.Timestamp, java.sql.Timestamp, java.sql.Timestamp, java.sql.Timestamp)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<FcarInout> queryByInOutTime(
			Timestamp miniInTime, Timestamp maxInTime, Timestamp miniOutTime,
			Timestamp maxOutTime) {
		// TODO Auto-generated method stub
		try{
			String hql = "from FcarInout as f where (f.getIn between ? and ?) and (f.getOut between ? and ?) and f.fcarCardId = ?";
			 return sessionFactory.getCurrentSession().createQuery(hql).
			               setParameter(0, miniInTime).setParameter(1, maxInTime).
			               setParameter(2, miniOutTime).setParameter(3,maxOutTime).list();
		}
		catch(Exception e){
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.FcarInoutDAOI#queryByInOutTimeAndFcardId(java.lang.String, java.sql.Timestamp, java.sql.Timestamp, java.sql.Timestamp, java.sql.Timestamp)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<FcarInout> queryByInOutTimeAndFcardId(String fcardId,
			Timestamp miniInTime, Timestamp maxInTime, Timestamp miniOutTime,
			Timestamp maxOutTime) {
		// TODO Auto-generated method stub
		 try{
				String hql = "from FcarInout as f where (f.getIn between ? and ?) and (f.getOut between ? and ?) and f.fcarCardId = ?";
				 return sessionFactory.getCurrentSession().createQuery(hql).
				               setParameter(0, miniInTime).setParameter(1, maxInTime).
				               setParameter(2, miniOutTime).setParameter(3,maxOutTime).
				               setParameter(4, fcardId).list();
			}
			catch(Exception e){
				return null;
			}
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.FcarInoutDAOI#queryLatestIn()
	 */
	@Override
	public FcarInout queryLatestIn(String fcardId) {
		// TODO Auto-generated method stub
		try{
			String hql = "from FcarInout as f where f.fcardId = ? order by f.getIn desc" ;
			return (FcarInout) sessionFactory.getCurrentSession().
					createQuery(hql).setParameter(0, fcardId).list().get(0);
		}
		catch(Exception e){
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.FcarInoutDAOI#queryLatestOut()
	 */
	@Override
	public FcarInout queryLatestOut(String fcardId) {
		// TODO Auto-generated method stub
		try{
			String hql = "from FcarInout as f where f.fcardId = ? order by f.getOut desc" ;
			return (FcarInout) sessionFactory.getCurrentSession().
					createQuery(hql).setParameter(0, fcardId).list().get(0);
		}
		catch(Exception e){
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.FcarInoutDAOI#queryByInTime(java.lang.String, java.sql.Timestamp)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<FcarInout> queryByInTime(String fcardId, Timestamp inTime) {
		// TODO Auto-generated method stub
		try{
			String hql = "from FcarInout as f where f.fcarCardId = ? and f.getIn = ?" ;
			return  sessionFactory.getCurrentSession().
					createQuery(hql).setParameter(0, fcardId).
					setParameter(1, inTime).list();
		}
		catch(Exception e){
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.FcarInoutDAOI#queryInByCardId(java.lang.String)
	 */
	@Override
	public FcarInout queryInByCardId(String cardId) {
		// TODO Auto-generated method stub
		try{
			String hql = "from FcarInout as f where f.fcarCardId = :cardId and f.state= :state" ;
			return (FcarInout) sessionFactory.getCurrentSession().
					createQuery(hql).setParameter("cardId", cardId).setParameter("state", "in").uniqueResult();
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}
